﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gamemaster.Naming;

namespace Gamemaster.Console
{
    public class NpcCommand : Command
    {
        public NpcCommand(params string[] args) : base(args)
        {  }

        public GenderOptions Gender { get; set; }
        public bool Married { get; set; }
        public bool Children { get; set; }

        protected override void Validate()
        {
        }
        protected override void RunCommand()
        {
            Out.Write(Gender);
            Out.WriteLine(" NPC");

            Npc npc = new Npc(Gender);

            if (Married)
            {
                GenderOptions spouseGender = Gender == GenderOptions.Male ?
                    GenderOptions.Female : GenderOptions.Male;

                npc.Spouse = new Npc(spouseGender);
            }

            if (Children)
            {
                for (int i = 0; i < Roll.d6(); i++)
                {
                    npc.Children.Add(new Npc(GenderOptions.Both));
                }
            }

            Out.WriteLine(npc);
        }
        protected override void HandleParameter(Parameter parameter)
        {
            parameter.Handled = true;

            string[] nameValuePair = parameter.Value.Split(new char[]{':', '='});
            string name = nameValuePair[0].ToLower();
            string value = nameValuePair.Length > 1 ? nameValuePair[1].Trim() : null;

            switch (name)
            {
                case "npc":
                    break;
                case "male":
                    Gender = GenderOptions.Male;
                    break;
                case "female":
                    Gender = GenderOptions.Female;
                    break;
                case "m":
                case "married":
                    Married = true;
                    break;
                case "c":
                case "children":
                    Children = true;
                    break;
                default:
                    parameter.Handled = false;
                    break;
            }
        }
    }

    public class Npc
    {
        public Name Name { get; set; }
        public Npc Spouse { get; set; }
        public List<Npc> Children { get; set; }

        public Npc(GenderOptions gender)
        {
            Name = Names.GenerateOne(gender);
            Children = new List<Npc>();
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder();

            builder.AppendLine(Name.ToString());

            if (Spouse != null)
            {
                if (Name.Gender == GenderOptions.Male)
                {
                    builder.Append("   his wife, ");
                }
                else
                {
                    builder.Append("   her husband, ");
                }
                builder.AppendLine(Spouse.Name.FirstName);
            }

            if (Children.Count > 0)
            {
                builder.AppendLine("   their children:");
            }
            Children.ForEach(c => builder.AppendFormat("      {0}", c));

            return builder.ToString();
        }
    }
}
